"""
数据采集模块包初始化文件

提供统一的数据采集接口
"""
import logging
from .stock_collector import StockCollector
from .futures_collector import FuturesCollector
from .crypto_collector import CryptoCollector
from .fund_collector import FundCollector
from .news_fundamental_collector import NewsFundamentalCollector
from .spot_collector import SpotCollector
from .forex_collector import ForexCollector

# 配置日志
logger = logging.getLogger("data_collector")


class DataCollector:
    """统一数据采集器接口"""
    
    def __init__(self):
        """初始化各类数据采集器"""
        self.stock_collector = StockCollector()
        self.futures_collector = FuturesCollector()
        self.crypto_collector = CryptoCollector()
        self.fund_collector = FundCollector()
        self.news_fundamental_collector = NewsFundamentalCollector()
        self.spot_collector = SpotCollector()
        self.forex_collector = ForexCollector()
    
    def collect_market_data(self, data_type, **kwargs):
        """
        统一的市场数据采集入口
        
        Args:
            data_type: 数据类型，如'stock', 'future', 'crypto', 'fund'等
            **kwargs: 特定数据类型需要的参数
        
        Returns:
            处理结果
        """
        logger.info(f"开始采集{data_type}数据，参数: {kwargs}")
        
        try:
            # 根据数据类型调用不同的采集器
            if data_type == "stock_list":
                # 股票列表信息采集
                return self.stock_collector.collect_stock_info()
            
            elif data_type == "stock_daily":
                # 股票日线数据采集
                return self.stock_collector.collect_daily_quote(
                    security_code=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
            
            elif data_type == "future_list":
                # 期货合约列表采集
                return self.futures_collector.collect_futures_info(
                    exchange_code=kwargs.get("exchange")
                )
            
            elif data_type == "future_daily":
                # 期货日线数据采集
                return self.futures_collector.collect_futures_daily_data(
                    symbol=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
            
            elif data_type == "crypto_list":
                # 加密货币列表采集
                return self.crypto_collector.collect_crypto_info(
                    limit=kwargs.get("limit", 200)
                )
            
            elif data_type == "crypto_daily":
                # 加密货币历史数据采集
                return self.crypto_collector.collect_crypto_history(
                    symbol=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
            
            elif data_type == "fund_list":
                # 基金列表采集
                return self.fund_collector.collect_fund_info(
                    fund_type=kwargs.get("fund_type", "all")
                )
            
            elif data_type == "fund_nav":
                # 基金净值数据采集
                return self.fund_collector.collect_fund_nav_history(
                    fund_code=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
            
            elif data_type == "financial_news":
                # 财经新闻采集
                return self.news_fundamental_collector.collect_financial_news(
                    source=kwargs.get("source", "sina"),
                    limit=kwargs.get("limit", 50)
                )
            
            elif data_type == "stock_notice":
                # 股票公告采集
                return self.news_fundamental_collector.collect_stock_notice(
                    stock_code=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date")
                )
            
            elif data_type == "macro_data":
                # 宏观经济数据采集
                return self.news_fundamental_collector.collect_macro_data(
                    indicator_name=kwargs.get("indicator"),
                    region=kwargs.get("region", "中国")
                )
            
            elif data_type == "financial_statement":
                # 财务报表数据采集
                return self.news_fundamental_collector.collect_financial_statement(
                    stock_code=kwargs.get("code"),
                    report_type=kwargs.get("report_type", "balance"),
                    period=kwargs.get("period", "年报")
                )
            
            elif data_type == "valuation":
                # 估值指标数据采集
                return self.news_fundamental_collector.collect_valuation_indicators(
                    stock_code=kwargs.get("code")
                )
            
            elif data_type == "spot_list":
                # 现货合约列表采集
                return self.spot_collector.collect_spot_info(
                    exchange=kwargs.get("exchange")
                )
                
            elif data_type == "spot_daily":
                # 现货日线数据采集
                return self.spot_collector.collect_spot_daily(
                    symbol=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
                
            elif data_type == "forex_list":
                # 外汇列表采集
                return self.forex_collector.collect_forex_info(
                    currency_type=kwargs.get("currency_type", "外汇")
                )
                
            elif data_type == "forex_daily":
                # 外汇日线数据采集
                return self.forex_collector.collect_forex_daily(
                    symbol=kwargs.get("code"),
                    start_date=kwargs.get("start_date"),
                    end_date=kwargs.get("end_date"),
                    task_id=kwargs.get("task_id")
                )
            
            else:
                logger.error(f"不支持的数据类型: {data_type}")
                return None
        
        except Exception as e:
            logger.error(f"采集{data_type}数据失败: {str(e)}")
            raise


# 单例模式
collector = DataCollector()